/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmCompopnente.java
 *
 * Created on 2-ago-2011, 10.28.18
 */
package UI;

import UI.clienti.dialogs.dlgClientiSearch;
import UI.componenti.dialogs.dlgComponentiSearch;
import classes.Componente;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;

/**
 *
 * @author mrossetti
 */
public class frmCompopnente extends javax.swing.JFrame {

    /** Creates new form frmCompopnente */
    private Componente Comp;
    public frmCompopnente() {
        initComponents();
        BufferedImage img = null;
        try{
            img = ImageIO.read(this.getClass().getResourceAsStream("/resources/componenti/exec.png"));
        }catch(Exception e){
            e.printStackTrace();
        }
        setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - getSize().width)/2, (Toolkit.getDefaultToolkit().getScreenSize().height - getSize().height)/2);
        setIconImage(img);
        Comp = new Componente();
        BoundComponente();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        P = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtIdComponente = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtCodice = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtNome = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        SP = new javax.swing.JScrollPane();
        txtNote = new javax.swing.JTextArea();
        cmdNuovo = new javax.swing.JButton();
        cmdSalva = new javax.swing.JButton();
        cmdCancella = new javax.swing.JButton();
        cmdEsci = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Componenti");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel1.setText("ID Componente");

        txtIdComponente.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtIdComponenteFocusLost(evt);
            }
        });

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("Codice");

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText("Nome");

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel4.setText("Note");

        txtNote.setColumns(20);
        txtNote.setRows(5);
        SP.setViewportView(txtNote);

        cmdNuovo.setText("Nuovo");
        cmdNuovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdNuovoActionPerformed(evt);
            }
        });

        cmdSalva.setText("Salva");
        cmdSalva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdSalvaActionPerformed(evt);
            }
        });

        cmdCancella.setText("Cancella");
        cmdCancella.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdCancellaActionPerformed(evt);
            }
        });

        cmdEsci.setText("Esci");
        cmdEsci.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdEsciActionPerformed(evt);
            }
        });

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/common/search.png"))); // NOI18N
        jButton1.setToolTipText("Ricerca componente");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout PLayout = new javax.swing.GroupLayout(P);
        P.setLayout(PLayout);
        PLayout.setHorizontalGroup(
            PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(PLayout.createSequentialGroup()
                        .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(SP, javax.swing.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE)
                            .addGroup(PLayout.createSequentialGroup()
                                .addComponent(txtIdComponente, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(txtCodice, javax.swing.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE)
                            .addComponent(txtNome, javax.swing.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE)))
                    .addGroup(PLayout.createSequentialGroup()
                        .addComponent(cmdNuovo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmdSalva)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmdCancella)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 114, Short.MAX_VALUE)
                        .addComponent(cmdEsci)))
                .addContainerGap())
        );
        PLayout.setVerticalGroup(
            PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtIdComponente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtCodice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(SP, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmdNuovo)
                    .addComponent(cmdSalva)
                    .addComponent(cmdCancella)
                    .addComponent(cmdEsci))
                .addContainerGap())
        );

        getContentPane().add(P, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cmdSalvaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdSalvaActionPerformed
        try{
            if(ControllaForm()){
                if(!txtIdComponente.getText().equals(""))Comp.setIDComponente(Integer.parseInt(txtIdComponente.getText()));
                Comp.setCodice_componente(txtCodice.getText());
                Comp.setNomeComponente(txtNome.getText());
                Comp.setNote(txtNote.getText());
                if(Comp.save()){
                    JOptionPane.showMessageDialog(this, "Salvataggio Effettuato", "The WareHouse", JOptionPane.INFORMATION_MESSAGE);
                    Comp.load(txtCodice.getText());
                    BoundComponente();
                }else{
                    JOptionPane.showMessageDialog(this, "Salvataggio non Effettuato","The WareHouse", JOptionPane.ERROR_MESSAGE);
                }
            }
        }catch(Exception e){
            JOptionPane.showMessageDialog(this, e.getMessage(),"The WareHouse", JOptionPane.ERROR_MESSAGE);
        }
        
    }//GEN-LAST:event_cmdSalvaActionPerformed

    private void cmdCancellaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCancellaActionPerformed
        if(Comp.getIDComponente() >= 0){
            if(JOptionPane.showConfirmDialog(this, "Si desidera cancellare il componente selezionato?","The WareHouse", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){
                if(Comp.delete()){
                    JOptionPane.showMessageDialog(this, "Componente cancellato", "The WareHouse", JOptionPane.INFORMATION_MESSAGE);
                }else{
                    JOptionPane.showMessageDialog(this, "Cancellazione annullata a causa di un errore sul database", "The WareHouse", JOptionPane.WARNING_MESSAGE);
                }
            }
        }
    }//GEN-LAST:event_cmdCancellaActionPerformed

    private void cmdEsciActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdEsciActionPerformed
        setVisible(false);
        dispose();
    }//GEN-LAST:event_cmdEsciActionPerformed

    private void txtIdComponenteFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtIdComponenteFocusLost
        try{
            Comp.setIDComponente(Integer.parseInt(txtIdComponente.getText()));
            Comp.load();
            if(Comp.getCodice_componente().equals("")){
                Comp = new Componente();
                BoundComponente();
            }else{
                BoundComponente();
            }
        }catch(Exception e){
            Comp = new Componente();
            BoundComponente();
        }
    }//GEN-LAST:event_txtIdComponenteFocusLost

    private void cmdNuovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdNuovoActionPerformed
        Comp = new Componente();
        BoundComponente();
    }//GEN-LAST:event_cmdNuovoActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        dlgComponentiSearch Search = new dlgComponentiSearch(this, true);
        Search.setVisible(true);
        Comp.load();
        BoundComponente();
    }//GEN-LAST:event_jButton1ActionPerformed
    
    public void setComponente(Componente C){
        Comp = C;
    }
    
    public Componente getComponente(){
        return Comp;
    }
    
    public void BoundComponente(){
        if(Comp.getIDComponente() >= 0){
            txtIdComponente.setText("" + Comp.getIDComponente());
        }else{
            txtIdComponente.setText("");
        }
        txtCodice.setText(Comp.getCodice_componente());
        txtNome.setText(Comp.getNomeComponente());
        txtNote.setText(Comp.getNote());
    }
    
    private boolean ControllaForm(){
        if(txtCodice.getText().equals("")){
           JOptionPane.showMessageDialog(this, "Inserire un codice componente", "The WareHouse", JOptionPane.WARNING_MESSAGE);
           return false;
        }
        Componente temp = new Componente();
        temp.load(txtCodice.getText());
        if(temp.getIDComponente() >= 0){
            if(JOptionPane.showConfirmDialog(this, "Attenzione!Codice componente già inserito, caricare i dati?", "The WareHouse", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.YES_OPTION){
                Comp = temp;
                BoundComponente();
                return false;
            }else{
                return false;
            }
        }
        
        return true;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel P;
    private javax.swing.JScrollPane SP;
    private javax.swing.JButton cmdCancella;
    private javax.swing.JButton cmdEsci;
    private javax.swing.JButton cmdNuovo;
    private javax.swing.JButton cmdSalva;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField txtCodice;
    private javax.swing.JTextField txtIdComponente;
    private javax.swing.JTextField txtNome;
    private javax.swing.JTextArea txtNote;
    // End of variables declaration//GEN-END:variables
}
